//
//  ViewController.m
//  Tom猫
//
//  Created by caimengjun on 16/12/11.
//  Copyright © 2016年 caimengjun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;
@end

@implementation ViewController

/*
animation 动画
Duration  速度
RepeatCount  重复次数
 StatusBarHidden  隐藏状态栏
*/
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//
-(BOOL)prefersStatusBarHidden {
    return YES;
}


//调用其他动作方法只需要下面 修改一行代码。
- (IBAction)clickStomachBtn:(id)sender {
    [self playAnimationWithButtonName :@"stomach" imageCount:34];
}

// 封装实现代码， 无需 每次都要 重复打这些代码。  只需  指定 特定的 图片路径 图片张数。

-(void) playAnimationWithButtonName: (NSString *) buttonName imageCount:(int) imageCount {
    
    
    //动画播放中 点击无效。等到播放完后才可以
    if (self.mainImageView.isAnimating ) return;
    
    
    //创一个数组存放 那些动作图片
    NSMutableArray *tempArrayM = [NSMutableArray array ];
    
    //
    for(int i=0;i<imageCount;i++) {
        
        //得到图片的名称
        NSString *imageFileName = [NSString stringWithFormat:@"%@_%02d.jpg",buttonName,i];
        
        //得到图片的路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:nil ];
        
        //创建image
        UIImage *image = [UIImage imageWithContentsOfFile:filePath];
        
        //把image塞进存放图片的数组 取出
        [tempArrayM addObject:image];
        
    }
    
    self.mainImageView.animationImages = tempArrayM;
    
    
    //动画播放速度 ， 次数
    self.mainImageView.animationDuration= self.mainImageView.animationImages.count *0.08;
    self.mainImageView.animationRepeatCount = 1;
    
    //开始播放
    [self.mainImageView startAnimating];
    
    //播放完毕后，清空animationImages
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((self.mainImageView.animationDuration + 0.2 )* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mainImageView.animationImages = nil;
    });
}


@end
